<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var map, control;
    
    function test_initialize(t) {
        t.plan( 2 );
    
        control = new OpenLayers.Control.OverviewMap();
        t.ok( control instanceof OpenLayers.Control.OverviewMap, 
            "new OpenLayers.Control.OverviewMap returns object" );
        t.eq( control.displayClass,
            "olControlOverviewMap", "displayClass is correct" );
    }

    function test_divs_title(t) {
        t.plan(2);
    
        control = new OpenLayers.Control.OverviewMap({
            maximizeTitle: "maximize title",
            minimizeTitle: "minimize title"
        });
        map = new OpenLayers.Map('map', {
            layers: [new OpenLayers.Layer("layer", {isBaseLayer: true})],
            controls: [control]
        });
        map.zoomToMaxExtent();
        t.eq(control.maximizeDiv.title, "maximize title", "maximizeDiv.title is correct");
        t.eq(control.minimizeDiv.title, "minimize title", "minimizeDiv.title is correct");
        map.destroy();
    }

    function test_setMap(t) {
        t.plan(4);

        var setMapTest = function(map) {
            t.ok(true, 
                "Handler.setMap called for " + this.CLASS_NAME);
            this.map = map;        
        };
        var drag_setMap = OpenLayers.Handler.Drag.prototype.setMap;
        OpenLayers.Handler.Drag.prototype.setMap = setMapTest;
        var click_setMap = OpenLayers.Handler.Click.prototype.setMap;
        OpenLayers.Handler.Click.prototype.setMap = setMapTest;

        map = new OpenLayers.Map('map', {
            layers  : [new OpenLayers.Layer("layer", {isBaseLayer: true})],
            controls: []
        });
        control = new OpenLayers.Control.OverviewMap();
    
        map.addControl(control);

        map.zoomToMaxExtent();
        t.eq(control.handlers.drag.map.id, control.ovmap.id,
            "drag.map is correct");
        t.eq(control.handlers.click.map.id, control.ovmap.id,
            "click.map is correct");

        map.destroy();
        OpenLayers.Handler.Drag.prototype.setMap = drag_setMap;
        OpenLayers.Handler.Click.prototype.setMap = click_setMap;
    }

    function test_destroy(t) {
        t.plan(6);

        // set up

        var log_drag = [], log_click = [], control;

        map = new OpenLayers.Map('map');
        map.addLayer(new OpenLayers.Layer("layer", {isBaseLayer: true}));

        control = new OpenLayers.Control.OverviewMap();
        map.addControl(control);

        map.zoomToMaxExtent();

        control.handlers.drag.destroy = function() {
            log_drag.push({"map": !!this.map.events});
        };
        control.handlers.click.destroy = function() {
            log_click.push({"map": !!this.map.events});
        };

        // test

        control.destroy();
        t.eq(log_drag.length, 2,
            "destroy() destroys drag handler twice, expected");
        if (log_drag.length == 2) {
            t.eq(log_drag[0].map, true,
                "destroy() destroys drag handler before ovmap is destroyed (0)");
            t.eq(log_drag[1].map, false,
                "destroy() destroys drag handler after ovmap is destroyed (1)");
        }
        t.eq(log_click.length, 2,
            "destroy() destroys click handler twice, expected");
        if (log_click.length == 2) {
            t.eq(log_click[0].map, true,
                "destroy() destroys click handler before ovmap is destroyed (0)");
            t.eq(log_click[1].map, false,
                "destroy() destroys click handler after ovmap is destroyed (1)");
        }

        // tear down
        map.destroy();
    }
    
    function test_addControl (t) {
        t.plan( 6 );
        map = new OpenLayers.Map('map');
        control = new OpenLayers.Control.OverviewMap();
        t.ok( control instanceof OpenLayers.Control.OverviewMap, 
            "new OpenLayers.Control.OverviewMap returns object" );
        t.ok( map instanceof OpenLayers.Map, 
            "new OpenLayers.Map creates map" );
        map.addControl(control);
        t.ok( control.map === map, 
            "Control.map is set to the map object" );
        t.ok( map.controls[4] === control, 
            "map.controls contains control" );
        t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 5,
            "Control div zIndexed properly" );
        t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 5,
            "Viewport div contains control div" );

        map.destroy();
    }
    
    function test_control_events (t) {
        t.plan( 10 );
        
        map = new OpenLayers.Map('map', {
            // when we recenter, don't waste time animating the panning
            // without this, the test fails in Firefox 10.0.1 on Linux
            panMethod: null,
            layers: [ new OpenLayers.Layer('Test Layer', {isBaseLayer: true}) ]
        });

        control = new OpenLayers.Control.OverviewMap();
        map.addControl(control, new OpenLayers.Pixel(20,20));

        var centerLL = new OpenLayers.LonLat(-71,42); 
        map.setCenter(centerLL, 11);

        t.delay_call(
            0.1, 
            function() {
                var overviewCenter = control.ovmap.getCenter();
                var overviewZoom = control.ovmap.getZoom();
                t.eq(overviewCenter.lon, -71, 
                    "OverviewMap center lon correct");
                t.eq(overviewCenter.lat, 42, 
                    "OverviewMap center lat correct");
                t.eq(overviewZoom, 8, 
                    "OverviewMap zoom correct");
                
                control.mapDivClick({'xy':new OpenLayers.Pixel(5,5)});
            },
            0.1,
            function() {
                var cent = map.getCenter();
                t.eq(cent.lon, -71.3515625, 
                    "Clicking on OverviewMap has correct effect on map lon");
                t.eq(cent.lat, 42.17578125,
                    "Clicking on OverviewMap has correct effect on map lat");
        
                control.handlers.drag = {
                    last: new OpenLayers.Pixel(5,5),
                    destroy: function() {}
                };
                control.rectDrag(new OpenLayers.Pixel(15, 15));
                control.updateMapToRect();
            },
            0.1,
            function() {
                var cent = map.getCenter();
                t.eq(cent.lon, -71.2734375, 
                    "Dragging on OverviewMap has correct effect on map lon");
                t.eq(cent.lat, 42.09765625, 
                    "Dragging on OverviewMap has correct effect on map lat");
                
                map.setCenter(new OpenLayers.LonLat(0,0), 0);
                var overviewCenter = control.ovmap.getCenter();
                var overviewZoom = control.ovmap.getZoom();
                t.eq(overviewCenter.lon, 0, 
                    "OverviewMap center lon correct -- second zoom");
                t.eq(overviewCenter.lat, 0,
                    "OverviewMap center lat correct -- second zoom");
                t.eq(overviewZoom, 0,
                    "OverviewMap zoomcorrect -- second zoom");
                map.destroy();
            }
        );
    }

    function test_initialize_maximized(t) {
        t.plan(4);

        control = new OpenLayers.Control.OverviewMap()
        map = new OpenLayers.Map('map', {
            layers  : [new OpenLayers.Layer("layer", {isBaseLayer: true})],
            controls: [control]
        });
        
        t.eq(control.maximized, false, 
            "OverviewMap is not maximized by default");
        t.eq(control.element.style.display, 'none', 
            "OverviewMap.element is not visible");
        map.destroy();

        control = new OpenLayers.Control.OverviewMap({
            maximized: true
        })
        map = new OpenLayers.Map('map', {
            layers  : [new OpenLayers.Layer("layer", {isBaseLayer: true})],
            controls: [control]
        });
        t.eq(control.maximized, true,
            "OverviewMap.maximized is set");
        t.eq(control.element.style.display, '', 
            "OverviewMap.element is visible");

        map.destroy();
    }
    
    function test_custom_div(t) {
        t.plan(3);
        var div = document.createElement('div');
        
        control = new OpenLayers.Control.OverviewMap({
          div: div
        });
        
        map = new OpenLayers.Map('map', {
            layers  : [new OpenLayers.Layer("layer", {isBaseLayer: true})],
            controls: [control]
        });
        
        t.eq(control.maximizeDiv, null, 
            "OverviewMap does not create maximize div");
        t.eq(control.minimizeDiv, null, 
            "OverviewMap does not create minimize div");
        
        var exc;
        try {
            control.maximizeControl();
            control.minimizeControl();
        } catch(e) {
            exc = e;
        }

        t.eq(exc, undefined, 'maximize and minimize do not trigger an exception');
        
        map.destroy();
    }

  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"></div>
</body>
</html>
